package com.zzf.concurrent.example.design.pattern.active.object;

import lombok.extern.slf4j.Slf4j;

/**
 * @author GaoFeng2017
 * @date 2019/9/11 14:29
 */
@Slf4j
public class FutureResult implements Result {

    private volatile Result result;
    private volatile boolean isDone;

    public FutureResult() {
        this.isDone = false;
    }

    public synchronized void setResultValue(Result result) {
        this.result = result;
        this.isDone = true;
        this.notifyAll();
    }

    @Override
    public synchronized Object getResultValue() {

        while (!isDone) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                log.info("fail to get value", e);
            }
        }

        return result.getResultValue();
    }
}
